import VueRouter from 'vue-router'
import store from '@/store/index.js'

const routes = [{
  path: '/',
  redirect: '/login'
}, {
  path: '/management',
  name: 'management',
  component: () => import(/* webpackChunkNAME: "artisan" */ '../views/management'),
  meta: {
    title: '管理系统',
    requiresAuth: true
  }
}, {
  path: '/login',
  name: 'login',
  component: () => import(/* webpackChunkNAME: "artisan" */ '../views/management/login'),
  meta: {
    title: '登录',
    requiresAuth: false
  }
},]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  // 检查是否需要登录
  if (to.meta.requiresAuth && !store.state.user) {
    console.log('user为空，跳转到登陆页面')
    next('/login') // 如果需要登录但未登录，则重定向到登录页面
  } else {
    next()
  }
})

export default router
